package edu.washington.cs.workflow.matching;

import java.util.Collection;

import com.ibm.wala.ipa.callgraph.CGNode;

import edu.washington.cs.workflow.analysis.AnalysisBuilder;
import edu.washington.cs.workflow.analysis.AnalysisBuilder.CG;
import edu.washington.cs.workflow.analysis.AnalysisBuilder.UIFramework;
import edu.washington.cs.workflow.util.Utils;
import junit.framework.TestCase;

public class TestMatchingJabRef extends TestCase {

	public void testFindMethodInJabRef() {
		String[] pkgs = new String[]{"net.sf.jabref."};
		String oldDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\jabref2.0\\JabRef-2.0-binary.jar";
		String newDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\jabref2.8\\JabRef-2.8.1.jar";
		String main = "Lnet/sf/jabref/JabRef";
		
		AnalysisBuilder oldBuilder = new AnalysisBuilder(oldDir, main, pkgs);
		oldBuilder.setCGType(CG.RTA);
		oldBuilder.setUIFramework(UIFramework.SWING);
		oldBuilder.buildAnalysis();
		
		AnalysisBuilder newBuilder = new AnalysisBuilder(newDir, main, pkgs);
		newBuilder.setCGType(CG.RTA);
		newBuilder.setUIFramework(UIFramework.SWING);
		newBuilder.buildAnalysis();
		
		//workflow 1
		String sig1 = "net.sf.jabref.SearchManager2.startSearch()V";
		//workflow 2
		String sig2 = "net.sf.jabref.imports.ImportMenuItem.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		String sig2new = "net.sf.jabref.imports.ImportFormats$1ImportAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		//workflow 3
		String sig3 = sig2;
		
		CGNode node1 = oldBuilder.findCGNodeBySignature(sig1);
		System.out.println(node1);
		Utils.checkNotNull(node1);
		
		CGNode node2 = oldBuilder.findCGNodeBySignature(sig2);
		System.out.println(node2);
		Utils.checkNotNull(node2);
		
		CGNode node2new = newBuilder.findCGNodeBySignature(sig2new);
		System.out.println(node2new);
		Utils.checkNotNull(node2new);
		
		CGNode node3 = node2;
		Utils.checkNotNull(node3);
		
		
        ExecutionTraceMatcher matcher = new ExecutionTraceMatcher(oldBuilder, newBuilder);
		CGNode matchedNode1 = matcher.findMatchedMethodByName(node1);
		System.out.println("Matched node 1: " + matchedNode1);
		CGNode counterpart1 = matcher.findCounterpart(node1);
		Utils.checkNotNull(counterpart1);
		
		//testing purpose
		CGNode tmp = newBuilder.findCGNodeBySignature(sig2);
		Utils.checkNotNull(tmp);
		
		CGNode mostSimilarMethod = matcher.findMostSimilarMethodByMethodName(node2);
		System.out.println(mostSimilarMethod);
		Utils.checkNotNull(mostSimilarMethod);
		
//		matcher.setSimilarNumber(20);
//		Collection<CGNode> colls = matcher.findAllSimilarMethodsByMethodName(node2);
//		for(CGNode c : colls) {
//			System.out.println(" -- " + c);
//		}
	}
	
}
